package com.health.trans.controller;

import cn.hutool.core.collection.CollUtil;
import com.health.trans.common.entity.ApiResponse;
import com.health.trans.util.DataSourceUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.Set;

/**
 * @author qyh
 */
@Api(tags = "获取数据库信息")
@RestController
@Validated
@Slf4j
@RequestMapping("view")
public class ViewController {


    @GetMapping(value = "/getDadaBases")
    public ApiResponse<Set<String>> getDadaBases() {
        Set<String> data = CollUtil.newHashSet( DataSourceUtil.DS_MAP.keySet());
        data.add("elastic");
        return ApiResponse.ok(data);
    }

    @GetMapping(value = "/getTableNames")
    public Object getTableNames(@NotBlank String dsName) {
        //if ("elastic".equals(dsName)){
        //    Set<String> collect = EsClassOperations.ES_CLASSES.parallelStream().map(Class::getSimpleName)
        //            .map(WordUtils::uncapitalize).collect(Collectors.toSet());
        //    return ApiResponse.ok(collect);
        //}
        return ApiResponse.ok(DataSourceUtil.tablesMap.get(dsName));
    }

    @GetMapping(value = "/getFieldNames")
    public Object getFieldNames(@NotBlank String dsName, @NotBlank String tableName) {
        return ApiResponse.ok(DataSourceUtil.getFieldsByTableName(dsName, tableName));
    }

}
